//
//  main.m
//  OC6_省市区限时代码
//
//  Created by dllo on 15/11/2.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {

    
    NSString *path = @"/Users/dllo/Desktop/省市区/area.txt";
    NSString *str = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    NSArray *arr = [str componentsSeparatedByString:@"\n"];
    
    
    NSMutableArray *provinceArr = [NSMutableArray array];
    
    for (NSInteger i = 0; i < arr.count; i++) {
        NSString *s = [arr objectAtIndex:i];
        if (![s hasPrefix:@" "]) {
            
            NSMutableArray *cityArray = [NSMutableArray array];
            NSMutableDictionary *provinceDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:s, @"name", cityArray, @"cityArray", nil];
            [provinceArr addObject:provinceDic];
        }
        else if (![s hasPrefix:@"   "]){
            
            NSMutableDictionary *provinceDic = [provinceArr lastObject];
            NSMutableArray *cityArray = [provinceDic objectForKey:@"cityArray"];
            NSMutableArray *areaArray = [NSMutableArray array];
            NSMutableDictionary *cityDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:s, @"name", areaArray, @"areaArray", nil];
            [cityArray addObject:cityDic];
        }
        else if([s hasPrefix:@"    "]){
            NSMutableDictionary *provinceDIc = [provinceArr lastObject];
            NSMutableArray *cityArray = [provinceDIc objectForKey:@"cityArray"];
            NSMutableDictionary *cityDic = [cityArray lastObject];
            NSMutableArray *areaArray = [cityDic objectForKey:@"areaArray"];
            [areaArray addObject:s];
        }
    }
    
    
    
    //        输出方法
    NSData *data = [NSJSONSerialization dataWithJSONObject:provinceArr options:NSJSONWritingPrettyPrinted error:nil];
    
    NSString *str1 = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"%@", str1);
    
    
    
    return 0;
}
